package backend.rss;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import backend.rss.items.AuthorItem;
import backend.rss.items.CommentItem;
import backend.rss.items.Item;
import backend.rss.items.ThemeItem;

public class ItemsContainer
{
	private Map<String, AuthorItem> themeAuthors;
	private Map<String, ThemeItem> themeItems;
	private Map<String, Set<CommentItem>> commentItems;
	
	private String pageTitle;
	private int recordCount;
	private int startIndex;
	private int itemsPerPage;
	
	protected ItemsContainer()
	{
		themeAuthors 	= new HashMap<String, AuthorItem>();
		themeItems 		= new HashMap<String, ThemeItem>();
		commentItems	= new HashMap<String, Set<CommentItem>>();
	}
	
	public ItemsContainer(Map<String, AuthorItem> themeAuthors, Map<String, ThemeItem> themeItems, 
						  Map<String, Set<CommentItem>> commentItems)
	{
		this.themeAuthors 	= themeAuthors;
		this.themeItems 	= themeItems;
		this.commentItems	= commentItems;
	}
	
	/**
	 * @param pageTitle the pageTitle to set
	 */
	protected void setPageTitle(String pageTitle)
	{
		this.pageTitle = pageTitle;
	}

	/**
	 * @param recordCount the recordCount to set
	 */
	protected void setRecordCount(int recordCount)
	{
		this.recordCount = recordCount;
	}

	/**
	 * @param startIndex the startIndex to set
	 */
	protected void setStartIndex(int startIndex)
	{
		this.startIndex = startIndex;
	}

	/**
	 * @param itemsPerPage the itemsPerPage to set
	 */
	protected void setItemsPerPage(int itemsPerPage)
	{
		this.itemsPerPage = itemsPerPage;
	}

	protected void addItem(Item item)
	{
		if (item instanceof AuthorItem)
		{
			//if author already exists, just get the new item's theme items and append
			/*
			if (themeAuthors.containsKey(item.getId()))
			{
				AuthorItem aItem 		= (AuthorItem)item;
				AuthorItem storedAuthor = themeAuthors.get(item.getId());
				
				Set<String> themeIds	= aItem.getThemeItems();
				
				for (String s : themeIds) 
					storedAuthor.addThemeItem(s);
			}
			else themeAuthors.put(item.getId(), (AuthorItem) item);
			*/
			themeAuthors.put(item.getId(), (AuthorItem)item);
		}
		else 
		if (item instanceof ThemeItem)
		{
			themeItems.put(item.getId(), (ThemeItem) item);
		}
		else
		if (item instanceof CommentItem)
		{/*
			CommentItem cItem = (CommentItem) item;
			
			Set<CommentItem> comments = null;
			if (commentItems.containsKey(cItem.getThemeCommentedId()))
				comments = commentItems.get(cItem.getThemeCommentedId());
			else comments = new HashSet<CommentItem>(1);
			
			comments.add(cItem);
			
			commentItems.put(cItem.getThemeCommentedId(), comments);
	*/
		}
		//TODO throw exception
	}
	
	
	/**
	 * @return the pageTitle
	 */
	public String getPageTitle()
	{
		return pageTitle;
	}

	/**
	 * @return the recordCount
	 */
	public int getRecordCount()
	{
		return recordCount;
	}

	/**
	 * @return the startIndex
	 */
	public int getStartIndex()
	{
		return startIndex;
	}

	/**
	 * @return the itemsPerPage
	 */
	public int getItemsPerPage()
	{
		return itemsPerPage;
	}

	/**
	 * @return the themeAuthors
	 */
	public Map<String, AuthorItem> getThemeAuthors()
	{
		return themeAuthors;
	}

	/**
	 * @return the themeItems
	 */
	public Map<String, ThemeItem> getThemeItems()
	{
		return themeItems;
	}
	
	
	/**
	 * @return the commentItems
	 */
	public Map<String, Set<CommentItem>> getCommentItems()
	{
		return commentItems;
	}

	/**
	 * 
	 * @param id the author id
	 * @return the author corresponding to the id. May return null
	 */
	public AuthorItem getAuthor(String id)
	{
		return themeAuthors.get(id);
	}
	
	/**
	 * 
	 * @param id the theme id
	 * @return the theme corresponding to the id. May return null
	 */
	public ThemeItem getTheme(String id)
	{
		return themeItems.get(id);
	}
	
	public Map<ThemeItem, AuthorItem> formatForViewer()
	{
		Map<String, ThemeItem> themeItems = getThemeItems();	
		Map<ThemeItem, AuthorItem> themes = new HashMap<ThemeItem, AuthorItem>(themeItems.size());
		
		for (ThemeItem item : themeItems.values())
		{
		//	themes.put(item, getAuthor(item.getAuthor()));
			themes.put(item, item.getAuthor());
		}
		return themes;
	}
}
